package cn.teleus.common.util.token;

import com.alibaba.fastjson.util.Base64;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhour on 2017/5/5.
 */
public class JWTTokenUtils {
    private static String decodedBases64(String jwtEncoded) throws UnsupportedEncodingException {
        String token = jwtEncoded.replaceAll("Bearer ", jwtEncoded);
        String[] splitArray = token.split("\\.");
        return getJson(splitArray[1]);
    }

    private static String getJson(String strEncoded) throws UnsupportedEncodingException{
        byte[] decodedBytes = Base64.decodeFast(strEncoded);
        return new String(decodedBytes, "UTF-8");
    }

    public static String decode(String token) throws UnsupportedEncodingException {
        return decodedBases64(token);
    }
//    public static void main(String[] args) throws Exception {
//        String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJwYWQiLCJuYW1lIjoicGFkIiwic2hvd05hbWUiOiJwYWQiLCJzY29wZXMiOlsiUk9MRV9QQUQiXSwiaXNzIjoiaHR0cDovL3N2bGFkYS5jb20iLCJpYXQiOjE0OTM5NTQ1MDUsImV4cCI6MTQ5Mzk1NjMwNX0.rpHWdoMtk-VN5nhQmf1o9eflOFuHBv1MWeQfko9BqyK1ZajQODQpQV7keqwTeysIKFTUr080daO7N2cOGtqSCw";
//        token = token.replaceAll("Bearer ", token);
//        System.out.println(decodedBases64(token));
//    }
}
